package cn.tedu.micro.demo.rabbit.producer;

import cn.tedu.micro.demo.rabbit.serializer.MessageSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class AmqpDelayProducer<T> {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MessageSerializer<T> messageSerializer;
    /**
     * string exchange
     * string routingkey
     * T msg 业务对象
     * Integer delayTime(毫秒数)
     */
    public void sendDelay(String exchange,String routingKey,T msg,Integer delayTime){
        //1.组织message对象 body使用的自定义序列化 properties expiration
        byte[] body=messageSerializer.serialize(msg);
        MessageProperties properties=new MessageProperties();
        properties.setExpiration(delayTime+"");
        Message message=new Message(body,properties);
        //2.rabbitTemplate调用
        rabbitTemplate.convertAndSend(exchange,routingKey,message);
    }
}
